package com.ruoyi.fjtseac.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.fjtseac.domain.dto.EssayDto;
import com.ruoyi.fjtseac.domain.po.Essay;
import com.ruoyi.fjtseac.service.EssayService;
import com.ruoyi.fjtseac.util.aop.PageView;
import com.ruoyi.fjtseac.util.aop.RedisCacheEnable;
import com.ruoyi.fjtseac.util.aop.TableEnum;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * 文章Controller
 *
 * @author icyrain11
 * @date 2023-06-29
 */
@Controller
@RequestMapping("/essay" )
@Api(tags={"文章接口"})
public class EssayController extends BaseController {

    private EssayService essayService;
    final RedisCache redisCache;

    @Autowired
    public EssayController(EssayService essayService, RedisCache redisCache) {
        this.essayService = essayService;
        this.redisCache = redisCache;
    }

    /**
     * 新增文章
     *
     * @param essayDto
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('fjtseac:essay:add')")
    @PostMapping("/addEssay")
    @ResponseBody
    @ApiOperation(value = "添加文章")
    public AjaxResult addEssay(@ApiParam(value = "新增文章参数", required = true) @RequestBody EssayDto essayDto) {
        return essayService.addEssay(essayDto);
    }

    /**
     * 删除文章
     *
     * @param essayId
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('fjtseac:essay:remove')")
    @DeleteMapping(value = "/deleteEssayById")
    @ResponseBody
    @ApiOperation(value = "根据文章id删除文章信息")
    public AjaxResult deleteEssayById(@ApiParam(value = "文章id", required = true) @RequestParam Integer essayId) {
        return essayService.deleteEssayById(essayId);
    }

    /**
     * 修改文章
     *
     * @param essayDto
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('fjtseac:essay:update')")
    @PutMapping("/updateEssay")
    @ResponseBody
    @ApiOperation(value = "修改文章的信息")
    public AjaxResult updateEssay(@ApiParam(value = "新增文章参数", required = true) @RequestBody EssayDto essayDto) {
        return essayService.updateEssay(essayDto);
    }

    /**
     * 查询文章
     *
     * @param essayId
     * @return 结果
     */
    @GetMapping("/selectEssay")
    @ResponseBody
    @ApiOperation(value = "根据文章id查找文章信息")
    public AjaxResult selectEssay(@ApiParam(value = "文章id", required = true) @RequestParam Integer essayId) {
        return essayService.selectById(essayId);
    }

    /**
     * 查询所有文章
     *
     * @param pageNum,pageSize
     * @return 结果
     */

    @GetMapping("/selectAll")
    @ResponseBody
    @ApiOperation(value = "无条件查找所有的文章信息")
    public AjaxResult selectAll(Integer pageNum, Integer pageSize) {
       return essayService.selectAll(pageNum, pageSize);
    }

    /**
     * 按所属类型查询文章（各类型文章页面调用该接口获取文章列表）
     *
     * @param typeId,pageNUm,pageSize
     * @return 结果
     */

    @GetMapping("/selectEssayByType")
    @RedisCacheEnable
    @ResponseBody
    @ApiOperation(value = "通过文章类型查找文章信息")
    public AjaxResult selectEssayByType(@ApiParam(value = "文章类型", required = true) @RequestParam Integer typeId, @ApiParam(value = "文章页码", required = true) @RequestParam Integer pageNum, @ApiParam(value = "每页显示的数量", required = true) @RequestParam Integer pageSize) {
        return essayService.selectEssayByType(typeId, pageNum, pageSize);
    }

    /**
     * 按创建时间查询文章
     *
     * @param startTime,endTime,pageNum,pageSize
     * @return 结果
     */

    @GetMapping("/selectEssayByTime")
    @ResponseBody
    @ApiOperation(value = "通过文章创建时间查找文章信息")
    public AjaxResult selectEssayByTime(@ApiParam(value = "时间区间左边界", required = true) @RequestParam String startTime, @ApiParam(value = "时间区间右边界", required = true) @RequestParam String endTime,@ApiParam(value = "文章类型id", required = true) @RequestParam Integer typeId, @ApiParam(value = "文章页码", required = true) @RequestParam Integer pageNum, @ApiParam(value = "每页显示的数量", required = true) @RequestParam Integer pageSize) {
        return essayService.selectEssayByTime(startTime, endTime, typeId, pageNum, pageSize);
    }

    /**
     * 按标题查询文章
     *
     * @param title,pageNum,pageSize
     * @return 结果
     */

    @GetMapping("/selectEssayByTitle")
    @ResponseBody
    @ApiOperation(value = "通过文章标题查找文章信息")
    public AjaxResult selectEssayByTitle(@ApiParam(value = "文章标题", required = true) @RequestParam String title, @ApiParam(value = "文章页码", required = true) @RequestParam Integer pageNum, @ApiParam(value = "每页显示的数量", required = true) @RequestParam Integer pageSize) {
        return essayService.selectEssayByTitle(title, pageNum, pageSize);
    }

    @PreAuthorize("@ss.hasPermi('fjtseac:essay:scarousel')")
    @PutMapping("/carousel")
    @ResponseBody
    @ApiOperation(value = "选择轮播图")
    public AjaxResult setCarousel(@ApiParam(value = "选择首页轮播图文章id（可多选）", required = true, allowMultiple = true) @RequestParam Integer... essayIds) {
        return essayService.setCarousel(essayIds);
    }


    @GetMapping("/carousel")
    @ResponseBody
    @ApiOperation(value = "获取首页轮播图")
    public AjaxResult getCarousel() {
        return essayService.getCarouselEssays();
    }

    @PutMapping("/pv")
    @ResponseBody
    @ApiOperation("浏览量自增接口")
    public AjaxResult incrPV(@ApiParam("文章id") @RequestParam Integer id) {
        Integer pv = redisCache.incr(TableEnum.ESSAY.getTableName() + ":" + id);
        return AjaxResult.success(pv);
    }


    @GetMapping("/pv")
    @ResponseBody
    @ApiOperation("浏览量查询接口")
    public AjaxResult getPv(@ApiParam(value = "文章id", required = true) Integer id) {
        Integer pv = redisCache.getCacheObject(TableEnum.ESSAY.getTableName() + ":" + id);
        return AjaxResult.success(pv);
    }
}


